<div id='panel'>

<?php
if($_POST)
{
$sqlSelProd="SELECT * FROM tbl_product WHERE product_id='".$_POST['productid']."'";
$rstSelProd=mysql_query($sqlSelProd);
$rowSelProd=mysql_fetch_array($rstSelProd);

$productName=$rowSelProd['product_name'];
if($_POST['type']=="accept")
{
	if($_POST['section']=="WS")
	{
		$sqlSelProd="SELECT * FROM tbl_wholeseller_product WHERE product_id='".$_POST['productid']."'";
		$rstSelProd=mysql_query($sqlSelProd);
		$rowSelProd=mysql_fetch_array($rstSelProd);
		$productName=$rowSelProd['product_name'];
		$pmesssage=$_SESSION['user_name']." has accepted to the offer you have made on (<a href=index.php?products=".$_POST['productid'].">".$productName."</a>), please proceed to pay" ."<p> Message from Owner:".$_POST['replymsg']."</p>";
	
	}
	else if($_POST['section']=="WSS")
	{
		$sqlSelProd="SELECT * FROM tbl_whole_wishlist WHERE item_id='".$_POST['productid']."'";
		$rstSelProd=mysql_query($sqlSelProd);
		$rowSelProd=mysql_fetch_array($rstSelProd);
		
		$productName=$rowSelProd['product_name'];
		$pmesssage=$_SESSION['user_name']." has accepted to the offer you have made on (<a href=index.php?wholeproducts=".$_POST['productid'].">".$productName."</a>), please proceed to pay" ."<p> Message from Owner:".$_POST['replymsg']."</p>";
		
	}
	else
	{
		$pmesssage=$_SESSION['user_name']." has accepted to the offer you have made on (<a href=index.php?products=".$_POST['productid'].">".$productName."</a>), please proceed to pay" ."<p> Message from Owner:".$_POST['replymsg']."</p>";
	}
	
	$parentBargain=$_POST['bargain_id'];
	$sqlSelBargain = "SELECT * FROM tbl_bargain WHERE bargain_id='$parentBargain'";
	$rstSelBargain = mysql_query($sqlSelBargain) or die(mysql_error());
	$rowSelBargain = mysql_fetch_array($rstSelBargain);
										
	//getting seller id who accept the bargain
	if($rowSelBargain['status']=="reply") 
	{
		$sqlSelUsr = "SELECT user_id FROM tbl_bargain WHERE bargain_id='".$rowSelBargain['parent_id']."'";
		$rstSelUsr = mysql_query($sqlSelUsr) or die(mysql_error());
		$rowSelUsr = mysql_fetch_row($rstSelUsr);
		$barSellerId = $rowSelUsr[0];
	}
	else 
	{
		$barSellerId = $rowSelBargain['user_id'];
		$user_type_check=$rowSelBargain['user_type'];
		
		$rec_id=$rowSelBargain['user_id'];
		$sen_id=$rowSelBargain['owner_id'];
		
		
	}
	if($user_type_check!=3)
	{							
		//getting owner type
		$productOwner=$_POST['owner_id'];

		$ownerType = getUserType($productOwner);
		
		$today = date("Y-m-d");
		 $bargaindeadline = mktime(0, 0, 0, date("m"), date("d")+7, date("y")); 
		$bargaindeadline=date("Y-m-d G:i:s", $bargaindeadline); 
										
		//insert item to tbl_items table....
		$sqlInsItem = "INSERT INTO tbl_items SET product_id='".$rowSelBargain['item_id']."',seller_id='".$barSellerId."', owner_id='".$productOwner."',
		owner_type='".$ownerType."', price='".$rowSelBargain['offer_amount']."', commission='".$rowSelBargain['offer_commision']."', bargained='1', status='1',
		created_date='$today',bargain_duration='$bargaindeadline'";
		
		$rstInsItem = mysql_query($sqlInsItem) or die(mysql_error());
		if(mysql_affected_rows()>0) 
		{
									//select bargain id to update bargain status...
			$sqlSelBar = "SELECT bargain_id FROM tbl_bargain WHERE item_id='".$rowSelBargain['item_id']."' AND user_id='$barSellerId' AND owner_id='$productOwner'";
			$rstSelBar = mysql_query($sqlSelBar) or die(mysql_error());
			if(mysql_num_rows($rstSelBar)>0) 
			{
													
					while($rowSelBar = mysql_fetch_array($rstSelBar)) 
					{
						//updating bargain status...
						$sqlUpdBar = "UPDATE tbl_bargain SET bargain_status='1' WHERE bargain_id='".$rowSelBar[0]."' OR parent_id='".$rowSelBar[0]."'";													 
						$rstUpdBar = mysql_query($sqlUpdBar) or die(mysql_error());
					}										
			} 
		}
		
	}
	else
	{
		$pmesssage_pay="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Congratulation!<br/>
                  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You have accepted the offer.<br/>
Please make a payment within 48 hours or the offer will become void";
		$type_pay="Payment";
		$section_pay="WS";
		$sub_pay="Payment Reminder";
		mysql_query("insert into tbl_messages(senderid,recieverid,productid,subject,message,posteddate,status,type,section,bargain_id,commision) values('".$sen_id."','".$rec_id."','".$_POST['productid']."','".$sub_pay."','".$pmesssage_pay."','".date("Y-m-d")."','1','".$type_pay."','".$section_pay."','".$_POST['bargain_id']."','".$_POST['commision']."')");
	}
	$sub="Bargain Accepted";
}


else if($_POST['type']=="decline")
{
	if($_POST['section']=="M")
	{
	$pmesssage=$_SESSION['user_name']." has declined your offer towards (<a href=index.php?SellProduct=".$_POST['productid'].">".$productName."</a>), Please make another offer by clicking on the link to the product" ."<p> Message from Owner:".$_POST['replymsg']."</p>";
	
	}
	else if($_POST['section']=="W")
	{
	$sqlSelProd="SELECT * FROM tbl_wishlist WHERE wishlist_id='".$_POST['productid']."'";
	$rstSelProd=mysql_query($sqlSelProd);
	$rowSelProd=mysql_fetch_array($rstSelProd);
	
	$productName=$rowSelProd['product_name'];
	$pmesssage=$_SESSION['user_name']." has declined your offer towards (<a href=index.php?wishproduct=".$_POST['productid'].">".$productName."</a>), Please make another offer by clicking on the link to the product" ."<p> Message from Owner:".$_POST['replymsg']."</p>";
	
	}
	else if($_POST['section']=="WSS")
	{
	$sqlSelProd="SELECT * FROM tbl_whole_wishlist WHERE item_id='".$_POST['productid']."'";
	$rstSelProd=mysql_query($sqlSelProd);
	$rowSelProd=mysql_fetch_array($rstSelProd);
	
	$productName=$rowSelProd['product_name'];
	$pmesssage=$_SESSION['user_name']." has declined your offer towards (<a href=index.php?wholeproducts=".$_POST['productid'].">".$productName."</a>), Please make another offer by clicking on the link to the product" ."<p> Message from Owner:".$_POST['replymsg']."</p>";
	}
	else
	{
	if($_POST['section']=="WS")
	{
	$sqlSelProd="SELECT * FROM tbl_wholeseller_product WHERE product_id='".$_POST['productid']."'";
	$rstSelProd=mysql_query($sqlSelProd);
	$rowSelProd=mysql_fetch_array($rstSelProd);
	$productName=$rowSelProd['product_name'];
	}
	$pmesssage=$_SESSION['user_name']." has declined your offer towards (<a href=index.php?products=".$_POST['itemid'].">".$productName."</a>), Please make another offer by clicking on the link to the product" ."<p> Message from Owner:".$_POST['replymsg']."</p>";
	}
	$sub="Bargain Declined";
}
else if($_POST['type']=="counter")
{
	//echo $_POST['bargain_id'];
	//exit();
	mysql_query("update tbl_bargain set offer_amount='".$_POST['price']."',offer_commision='".$_POST['commision']."' where bargain_id='".$_POST['bargain_id']."'");
	
	if($_POST['section']=="M")
	{
	$type="Bargain";
	$section="C";
	if(trim($_POST['commision']))
	{
	$commision="Commision: $".number_format($_POST['commision'],2);
	}
	$pmesssage=$_SESSION['user_name']." has placed counter offer of ($".number_format($_POST['price'],2)." ".$commision ." towards (<a href=index.php?SellProduct=".$_POST['productid'].">".$productName."</a>)<p> Message from Owner:".$_POST['replymsg']."</p>";
	
	}
	else if($_POST['section']=="W")
	{
	$sqlSelProd="SELECT * FROM tbl_wishlist WHERE wishlist_id='".$_POST['productid']."'";
	$rstSelProd=mysql_query($sqlSelProd);
	$rowSelProd=mysql_fetch_array($rstSelProd);
	$productName=$rowSelProd['product_name'];
	$type="Bargain";
	$section="C";
	$pmesssage=$_SESSION['user_name']." has placed counter offer of ($".number_format($_POST['price'],2)." towards (<a href=index.php?wishproduct=".$_POST['productid'].">".$productName."</a>)<p> Message from Owner:".$_POST['replymsg']."</p>";
	
	}
	else if($_POST['section']=="WS")
	{
	$sqlSelProd="SELECT * FROM tbl_wholeseller_product WHERE product_id='".$_POST['productid']."'";
	$rstSelProd=mysql_query($sqlSelProd);
	$rowSelProd=mysql_fetch_array($rstSelProd);
	$productName=$rowSelProd['product_name'];
	$type="Bargain";
	$section="WS";
	$pmesssage=$_SESSION['user_name']." has placed counter offer of ($".number_format($_POST['price'],2)." towards (<a href=index.php?wholeproducts=".$_POST['productid'].">".$productName."</a>)<p> Message from Owner:".$_POST['replymsg']."</p>";
	
	}
	else if($_POST['section']=="WSS")
	{
	$type="Bargain";
	$section="WSS";
	$sqlSelProd="SELECT * FROM tbl_whole_wishlist WHERE item_id='".$_POST['productid']."'";
	$rstSelProd=mysql_query($sqlSelProd);
	$rowSelProd=mysql_fetch_array($rstSelProd);
	
	$productName=$rowSelProd['product_name'];
	$pmesssage=$_SESSION['user_name']." has placed counter offer of ($".number_format($_POST['price'],2)." towards (<a href=index.php?wholeproducts=".$_POST['productid'].">".$productName."</a>)<p> Message from Owner:".$_POST['replymsg']."</p>";
	
	
	}
	else
	{
	
	$type="Bargain";
	$section="C";
	if(trim($_POST['commision']))
	{
	$commision="Commision: $".number_format($_POST['commision'],2);
	}
	
	$pmesssage=$_SESSION['user_name']." has placed counter offer of ($".number_format($_POST['price'],2)." ".$commision." towards (<a href=index.php?products=".$_POST['itemid'].">".$productName."</a>)<p> Message from Owner:".$_POST['replymsg']."</p>";
	
	}
	$sub="Counter Offer";
}
else
{
$pmesssage=$_POST['replymsg'];
$sub=$_POST['replysub'];
}
if($_POST['parentid'])
{


mysql_query("insert into tbl_messages(senderid,recieverid,productid,subject,message,posteddate,status,type,bargain_id,commision) values('".$_SESSION['user_id_sess']."','".$_POST['parentid']."','".$_POST['productid']."','".$sub."','".$pmesssage."','".date("Y-m-d")."','1','".$type."','".$_POST['bargain_id']."','".$_POST['commision']."')");

}
mysql_query("insert into tbl_messages(senderid,recieverid,productid,subject,message,posteddate,status,type,section,bargain_id,commision) values('".$_SESSION['user_id_sess']."','".$_POST['recieverid']."','".$_POST['productid']."','".$sub."','".$pmesssage."','".date("Y-m-d")."','1','".$type."','".$section."','".$_POST['bargain_id']."','".$_POST['commision']."')");
$_SESSION['msg']="Message Sent Successfully";
header("Location:index.php?inbox");
}
  mysql_query("update tbl_messages set status='2'  where messageid='".$_GET['reply']."'");

  $sqlMsg="select * from tbl_messages where messageid='".$_GET['reply']."'";
  $resMsg=mysql_query($sqlMsg);
  $rowMsg=mysql_fetch_array($resMsg);
  
  $sqlseller=mysql_query("select * from tbl_messages where parentid='".$_GET['reply']."'");
  $rowseller=mysql_fetch_array($sqlseller);
  ?>
<form id="form1" name="form1" method="post" action="index.php?reply=<?php echo $_GET['reply'];?>">
  <table width="100%" border="0" cellpadding="5">
    <tr>
      <td><strong>Reply to this message </strong></td>
      <td align="right"><a href="index.php?inbox">My Inbox </a></td>
    </tr>
	<?php if($_GET['type']!="bargain")
	{?>
    <tr>
      <td width="76%" colspan="2"><input type="text" value="Re:<?php echo $rowMsg['subject'];?>" size="40" name="replysub"/></td>
    </tr>
	<?php } else { ?>
	<input type="hidden" value="Bargain Accepted" size="40" name="replysub"/>
	<?php } if($_GET['action']=="counter") {?>
	 <tr>
      <td width="76%" colspan="2">Counter Offer:<br /><input type="text" value="" size="10" name="price"/></td>
    </tr>
	<?php } if(($rowMsg['section']=="M" || $rowMsg['commision']!="") && $_GET['action']=="counter") {?>
    <tr>
      <td colspan="2">Commision:<br />
        <input type="text" value="" size="10" name="commision"/></td>
    </tr>
	<?php }?>
    <tr>
      <td colspan="2"><textarea cols="40" rows="6" name="replymsg"></textarea>
        <input name="recieverid" type="hidden" id="recieverid" value="<?php echo $rowMsg['senderid'];?>"/>
		<input name="productid" type="hidden" id="productid" value="<?php echo $rowMsg['productid'];?>"/>
		<input name="itemid" type="hidden" id="itemid" value="<?php echo $rowMsg['itemid'];?>"/>
		<input name="type" type="hidden" id="type" value="<?php echo $_GET['action'];?>"/>
		<input name="section" type="hidden" id="section" value="<?php echo $rowMsg['section'];?>"/>
		<input name="parentid" type="hidden" id="parentid" value="<?php echo $rowseller['recieverid'];?>"/>
		<input name="bargain_id" type="hidden" id="bargain_id" value="<?php echo $rowMsg['bargain_id'];?>"/>
		<input name="owner_id" type="hidden" id="owner_id" value="<?php echo $rowMsg['ownerid'];?>"/>				</td>
    </tr>
    <tr>
      <td colspan="2"><input type="submit" name="Submit" value="Post Reply" /></td>
    </tr>
  </table>
</form>
</div>
<br>